package com.lottery.obtainData;

import com.lottery.dao.qiuqiu.accoutDao.UserDao;
import com.lottery.dao.qiuqiu.lobbyDao.LotteryDao;
import com.lottery.dao.qiuqiu.lobbyDao.WinningResultsDao;
import com.lottery.dao.qiuqiu.wealthDao.AccountDao;
import com.lottery.dto.WinProperty;
import com.lottery.entity.Lottery;
import com.lottery.service.qiuqiu.JedisClient;
import com.lottery.service.qiuqiu.MsgService.MsgNoticeService;
import com.lottery.service.qiuqiu.lobbyService.LotteryDrawService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

public abstract class AchieveData implements Runnable {

    // 停止下注
    @Value("${STOPBET}")
    protected String STOPBET;

    // 开始下注
    @Value("${BEGAINBET}")
    protected String BEGAINBET;

    @Resource
    protected MsgNoticeService msgNoticeService;

    @Resource
    protected LotteryDao lotteryDao;

    @Resource
    protected JedisClient jedisClient;

    @Resource
    protected WinningResultsDao winningResultsDao;
    @Resource
    protected AccountDao accountDao;
    @Resource
    protected UserDao userDao;
    @Resource
    protected LotteryDrawService lotteryDrawService;


    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    public int lotteyType;

    @Override
    public void run() {
        WinProperty property = new WinProperty();
        try {
            logger.info("线程启动AchieveData:startThreaThrea");
            // 启动时立刻校对期号和对应开奖
            String nextDate = StartAction();

            // 休眠到下一期正常下注的时间
            ObtainDataUtils.sleep(nextDate, 30 * 1000);
            //停止下注信息
            //insertStopPromptNotice();
            ObtainDataUtils.sleep(nextDate, 0);
        } catch (Exception e) {
            logger.error("before kaijiang error", e);
        }
        //正常开奖流程
        while (true) {
            // 休眠间隔3.5秒
            try {
                Thread.sleep(3500);
            } catch (Exception e) {
                logger.info("信息:{}", "休眠异常,继续!");
                continue;
            }
            try {
                //开奖条件判断
                boolean flag = judgeWinBet(property);
                if (flag) {
                    continue;
                } else {
                    //1.获取并显示开奖号码
                    boolean winInfomation = getWinInfomation(property);
                    if (winInfomation) {
                        continue;
                    } else {
                        //显示中奖信息
                        insertPromptNotice(property);
                    }
                    //2.开奖
                    YesWinningFactory(property);
                    //3.显示中奖信息
                    //selectWinningResultsService(property);
                    //4.显示开始下注
                    //insertStartPromptNotice();

                    logger.info("************************start robot betting ************************");
                    //5.机器人开始下注
                    //robotBet(property);

                    // 6.休眠
                    String nextTime = property.getNextTime();
                    boolean sleep = ObtainDataUtils.sleep(nextTime, 30 * 1000);
                    if (sleep == false) {
                        logger.info("格式转换异常或休眠异常,结束本次循环");
                        continue;
                    }
                    //7.显示停止下注
                    //insertStopPromptNotice();
                    boolean sleepOne = ObtainDataUtils.sleep(nextTime, 0);
                    if (sleepOne == false) {
                        logger.info("格式转换异常或休眠异常,结束本次循环");
                        continue;
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("开奖error:", e);
                continue;
            }

        }

    }


    //显示中奖信息
    protected abstract void insertPromptNotice(WinProperty ssc);

    // 启动时立刻校对期号和对应开奖
    protected abstract String StartAction() throws Exception;

    //显示停止下注
    protected void insertStopPromptNotice() {
        msgNoticeService.insertAllCanalMsg(STOPBET, lotteyType);
    }

    ;

    //开奖条件判断
    protected abstract boolean judgeWinBet(WinProperty property);

    //1.获取并显示开奖号码
    protected abstract boolean getWinInfomation(WinProperty property);

    //4.显示开始下注
    protected void insertStartPromptNotice() {
        msgNoticeService.insertAllCanalMsg(BEGAINBET, lotteyType);
    }

    ;

    //5.机器人开始下注
    protected abstract void robotBet(WinProperty property);

    //3.显示中奖信息
    protected abstract void selectWinningResultsService(WinProperty property);

    //2.开奖
    protected abstract void YesWinningFactory(WinProperty ssc);


    // 将当前开奖信息插入数据库
    public Boolean insertLottery(String expect, int expectNum, String opencode, Date opentime,String str) {
        Lottery lottery = new Lottery();
        lottery.setExpect(expect);
        lottery.setExpectNum(expectNum);
        lottery.setOpencode(opencode);
        lottery.setOpentime(opentime);
        lottery.setInserttime(new Date());
        lottery.setLotteyType(lotteyType);
        lottery.setWinInfo(str);
        logger.info("插入数据库:{}", lottery);
        try {
            lotteryDao.insertLottery(lottery);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

}

